package com.banesco.precargadepositos.interfaz.componentes;

import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.FontFamily;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.Keypad;
import net.rim.device.api.ui.XYEdges;
import net.rim.device.api.ui.XYRect;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.decor.BackgroundFactory;
import net.rim.device.api.ui.decor.Border;
import net.rim.device.api.ui.decor.BorderFactory;

public class BotonBanesco extends ButtonField {

	private String label;

	private char _tipo;
	private String _posicion;
	

	int _inicioDegradadoAzul = 0X00ABDF;
	int _finDegradadoAzul = 0X00497E;
	
	int _inicioDegradadoVerde = 0X8DBAA4;
	int _finDegradadoVerde = 0X054022;
	
	int _inicioDegradadoGris = 0XD8D8D8;
	int _finDegradadoGris = 0X999999;
	
	int _tamanofuente;
	
	Bitmap _bitmapFondoBoton = Bitmap.getBitmapResource("img/BotonAzul2.png");

	public BotonBanesco(String texto, long style)
	{
		super(style);
		this.label = texto;
		setearFont();
	}
	
	
	public BotonBanesco(String texto, long style,FieldChangeListener accion)
	{
		super(style);
		this.label = texto;
		this.setChangeListener(accion);
		setearFont();
		
		setPadding(0,0,0,0);
		setMargin(10, 10, 10, 10);
	}

	/*
	 * Consutructor por Defecto, genera una nueva instancia de un boton con BackgroundImage
	 * Label: Texto del Boton
	 * Posicion: Horizontal, vertical, posicion del boton
	 * Tipo:
	 * Style, estilo
	 * esBotonAtras: define que botones, boton atras (unico diferente) o resto de botones
	 */
	public BotonBanesco(String label, boolean esBotonAtras,boolean rounded) {

		this.label = label;
		setearFont();
		
		if(esBotonAtras)
		{
			Bitmap _bitmapFondoBotonIzquierda = Bitmap.getBitmapResource("img/fondoBotonIzquierda.png");
			setBackground(BackgroundFactory.createBitmapBackground(_bitmapFondoBotonIzquierda,10,10,0));
			setBorder(VISUAL_STATE_ACTIVE, BorderFactory.createRoundedBorder(new XYEdges(0,0,0,0)));
			setBorder(VISUAL_STATE_NORMAL, BorderFactory.createRoundedBorder(new XYEdges(0,0,0,0)));
			setBorder(VISUAL_STATE_FOCUS, BorderFactory.createRoundedBorder(new XYEdges(0,0,0,0)));
			setBorder(VISUAL_STATE_DISABLED, BorderFactory.createRoundedBorder(new XYEdges(0,0,0,0)));
			setBorder(VISUAL_STATE_DISABLED_FOCUS, BorderFactory.createRoundedBorder(new XYEdges(0,0,0,0)));
			setPadding(5,10,0,10);
			setMargin(10, 5, 5, 1);
		}
		else
		{

		}

		if(rounded)
		{
			/*setBorder
				(VISUAL_STATE_ACTIVE, BorderFactory.createRoundedBorder(new XYEdges(10,10,10,10),0));
			setBorder
				(VISUAL_STATE_NORMAL, BorderFactory.createRoundedBorder(new XYEdges(10,10,10,10),0));
			setBorder
				(VISUAL_STATE_FOCUS, BorderFactory.createRoundedBorder(new XYEdges(10,10,10,10),0));
			 */
		}
		else
		{
			/*setBorder
				(VISUAL_STATE_ACTIVE, BorderFactory.createSimpleBorder(new XYEdges(10,10,10,10), 0));
			setBorder
				(VISUAL_STATE_NORMAL, BorderFactory.createSimpleBorder(new XYEdges(10,10,10,10), 0));
			setBorder
				(VISUAL_STATE_FOCUS, BorderFactory.createSimpleBorder(new XYEdges(10,10,10,10), 0));
			 */
		}

		setPadding(0,0,0,0);
		setMargin(10, 10, 10, 10);


	}


	/*
	 * Consutructor por Defecto
	 */
	public BotonBanesco() {
		
		setearFont();
		setPadding(0,0,0,0);
		setMargin(10, 10, 10, 10);
	}

	public int getPreferredHeight() {
		return getFont().getHeight() + 8;
	}

	public int getPreferredWidth() {
		return getFont().getAdvance(label) + 8;
	}

	protected void layout(int width, int height) {
		setExtent(Math.min(width, getPreferredWidth()), Math.min(height, 
				getPreferredHeight()));
	}

	protected void paint(Graphics graphics) {
		
		graphics.setGlobalAlpha(255); // Sin transparencia
		
		if(!isEditable())
			setBackground(BackgroundFactory.createLinearGradientBackground(_inicioDegradadoGris, _inicioDegradadoGris, _finDegradadoGris, _finDegradadoGris));
		else
			setBackground(BackgroundFactory.createLinearGradientBackground(_inicioDegradadoAzul, _inicioDegradadoAzul, _finDegradadoAzul, _finDegradadoAzul));
			//setBackground(BackgroundFactory.createBitmapBackground(_bitmapFondoBoton,10,10,0));
			
		setBorder(VISUAL_STATE_ACTIVE, BorderFactory.createRoundedBorder(new XYEdges(2,2,2,2)));
		setBorder(VISUAL_STATE_NORMAL, BorderFactory.createRoundedBorder(new XYEdges(2,2,2,2)));
		setBorder(VISUAL_STATE_FOCUS, BorderFactory.createRoundedBorder(new XYEdges(2,2,2,2)));
		setBorder(VISUAL_STATE_DISABLED, BorderFactory.createRoundedBorder(new XYEdges(2,2,2,2)));
		setBorder(VISUAL_STATE_DISABLED_FOCUS, BorderFactory.createRoundedBorder(new XYEdges(2,2,2,2)));
	
		setPadding(5,15,5,15);
		
		if (isFocus() && isEditable()) {
			
			graphics.setColor(Color.LIGHTBLUE);
			graphics.drawText(label, 4, 4);

		}
		else {
			graphics.setColor(Color.WHITE);
			graphics.drawText(label, 4, 4);

		}
	}

		
	public boolean isFocusable() {
		return true;
	}

	protected void drawFocus(Graphics graphics, boolean on) 
	{
		
		graphics.setGlobalAlpha(255); // Sin transparencia
		
		graphics.setColor(Color.BLUE);
		setBorder(BorderFactory.createRoundedBorder(new XYEdges(2,2,2,2)));
		graphics.setBackgroundColor(Color.GRAY);
		
		Border bordeSeleccionado = new Border(new XYEdges(2,2,2,2),(int) Field.NON_FOCUSABLE)
		{
			public void paint(Graphics graphics, XYRect rect) 
			{
				graphics.setColor(Color.BLACK);
			}
		};
		 this.setBorder(bordeSeleccionado);
	}

	protected void onFocus(int direction) {
		super.onFocus(direction);  
		invalidate();
	}

	protected void onUnfocus() {
		super.onUnfocus();
		invalidate();
	}

	//	  protected boolean navigationClick(int status, int time) {
	//		  if (_tipo == 'A')
	//			  _presentador.irAPantallaAceptar();
	//		  else if(_tipo == 'C')
	//			  _presentador.irAPantallaCancelar();
	//	    return true;
	//	  }

	protected boolean keyChar(char character, int status, int time) {
		if (character == Keypad.KEY_ENTER) {
			fieldChangeNotify(0);
			return true;
		}
		return super.keyChar(character, status, time);
	}

	
	public String getLabel() {
		return this.label;
	}
	
	
	/**
	 * Metodo que setea el label con texto del boton
	 */
	public void setLabel(String texto) {
		this.label = texto;
	}

	private void setearFont()
	{
		try 
		{
			if(Display.getWidth() < 480)
				_tamanofuente = 17;
			else
				_tamanofuente = 26;
			
			String fontFamily = "BBAlpha Sans";
			FontFamily fontFamily1;
			fontFamily1 = FontFamily.forName(fontFamily);
			this.setFont(fontFamily1.getFont(Font.BOLD,_tamanofuente));
		} catch (ClassNotFoundException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
